local test = UnitTest:New("03DongleModules")
local Dongle = DongleStub("Dongle-1.0")

function test:dongle_new_module()
	assert_fails(function() Dongle.NewModule({}) end, "You must call 'NewModule' from a registered Dongle.")
	assert_fails(function() Dongle:NewModule(1) end, argerror(2))
	assert_fails(function() Dongle:NewModule(nil, 1) end, argerror(2))
	local obj,name = Dongle:New("Gamma")
	local obj,name = obj:NewModule("Gamma-Alpha")
	assert(type(obj) == "table")
	assert(name == "Gamma-Alpha")
end

function test:dongle_has_module()
	assert_fails(function() Dongle.HasModule({}) end, "You must call 'HasModule' from a registered Dongle.")
	assert_fails(function() Dongle:HasModule(1) end, argerror(2))
	local obj,name = Dongle:New("Delta")
	Dongle:New("Delta-Gamma")
	assert(not obj:HasModule("Delta-Alpha"))

	local module,modulename = obj:NewModule("Delta-Alpha")
	assert(obj:HasModule("Delta-Alpha"))
	assert(not obj:HasModule("Delta-Beta"))
	assert(not obj:HasModule("Delta-Gamma"))
end

function test:dongle_iterate_modules()
	assert_fails(function() Dongle.IterateModules({}) end, "You must call 'IterateModules' from a registered Dongle.")
	assert(type(Dongle:IterateModules()) == "function")
	local obj,name = Dongle:New("Epsilon")
	local i = 0
	for k in obj:IterateModules() do
		i = i + 1
	end
	assert(i == 0)
	local epsilongamma = obj:NewModule("Epsilon-Gamma")
	local epsilondelta = obj:NewModule("Epsilon-Delta")
	local modtbl = {}
	for name,module in obj:IterateModules() do
		modtbl[name] =  module
	end
	local count = 0
	for name,module in obj:IterateModules() do
		count = count + 1
	end

	assert(modtbl["Epsilon-Delta"] == epsilondelta)
	assert(modtbl["Epsilon-Gamma"] == epsilongamma)
	assert(count == 2)
end

test:Run()